jQuery.extend({
    fetchXMLDocument: function (res) {
        var doc;
        try {
            doc = res.responseXML.documentElement;
        } catch(ex) {
            doc = (new DOMParser()).parseFromString(res.responseText, "text/xml").documentElement;
        }
        return doc;
    }
});

jQuery.fn.extend({
    safeloadBlock: function(url, params, callback) {
        blockUI();

        this.safeload(url, params,
                function (responseText, status) {
                    unblockUI();
                    $.sessionTimeout();

                    if (callback) {
                        callback(responseText, status);
                    }
                },
                null,
                function (response, status) {
                    alert($(" //message", jQuery.fetchXMLDocument(response)).text());
                    unblockUI();
                });
    },

    safeload: function( url, params, callback, ifModified, onerror ) {
		callback = callback || function(){};

//		var type = "GET";

        var type = "POST";
		// If the second parameter was provided
		if ( params ) {
            // If it's a function
			if ( jQuery.isFunction(params) ) {
				// We assume that it's the callback
				callback = params;
				params = {};
			}
		} else {
            params = {};
        }

        params.ajaxRequest = true;

        params = jQuery.param( params );

        onerror = onerror || function (response, status) {
            alert($(" //message", jQuery.fetchXMLDocument(response)).text());
        };

        var self = this;

        // Request the remote document
		jQuery.ajax({
			url: url,
			type: type,
			data: params,
			ifModified: ifModified,
			complete: function (res, status) {
				if ( status == "success" || !ifModified && status == "notmodified" ) {
                    if (res.responseText.startsWith("<error>")) {
                        onerror(res, status);
                    } else {
                        // Inject the HTML into all the matched elements
                        self.attr("innerHTML", res.responseText)
                                // Execute all the scripts inside of the newly-injected HTML
                                .evalScripts()
                                // Execute callback
                                .each( callback, [res.responseText, status, res] )
                                // Make things hoverable
//                                .hoverable();
                   }
                } else {          
                    onerror(res, status);
                }
			},
            beforeSend: function (xml) {
                xml.setRequestHeader("Cache-Control", "no-cache");
                xml.setRequestHeader("Pragma", "no-cache");
            }
        });
		return this;
	},
    isEmpty: function () {
        var text = this.text();
        return text == '\u00A0' || text == "";
    },
    makeEmpty: function () {
        this.html('\u00A0');
    }
});

$.ajaxSetup({
    contentType: "application/x-www-form-urlencoded;charset=UTF-8"
});            
